package com.cskaoyan.service.vx.wangdi;

import com.cskaoyan.bean.Address;
import com.cskaoyan.bean.AddressExample;
import com.cskaoyan.bean.Region;
import com.cskaoyan.bean.RegionExample;
import com.cskaoyan.bo.DataBo;
import com.cskaoyan.bo.vx.wangdi.DetailBo;
import com.cskaoyan.bo.vx.wangdi.RegionListBo;
import com.cskaoyan.bo.vx.wangdi.VxAddressBo;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VxAddressServiceImpl  implements VxAddressService{
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;
    @Override
    public VxAddressBo list() {
        List<DataBo> addresses = addressMapper.list1();
        VxAddressBo vxAddressBo = new VxAddressBo();
        vxAddressBo.setAddresses(addresses);
        return vxAddressBo;
    }

    @Override
    public void delete(Integer id) {
        addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public RegionListBo regionList(Integer pid) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<Region> regions = regionMapper.selectByExample(regionExample);
        RegionListBo regionListBo = new RegionListBo();
        regionListBo.setRegions(regions);
        return regionListBo;
    }

    @Override
    public void sava(Address address) {
        if (address.getId() == 0) {
            AddressExample addressExample = new AddressExample();
            addressMapper.insertSelective(address);
        }else {

            addressMapper.updateByPrimaryKeySelective(address);
        }
    }

    @Override
    public DetailBo detail(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        Region region = regionMapper.selectByPrimaryKey(address.getCityId());
        Integer pid = region.getPid();
//        RegionExample regionExample = new RegionExample();
//        RegionExample.Criteria criteria = regionExample.createCriteria();
//        criteria.andTypeEqualTo(type);
//        List<Region> regions = regionMapper.selectByExample(regionExample);
        Region region1 = regionMapper.selectByPrimaryKey(pid);
        DetailBo detailBo = new DetailBo();
        detailBo.setDefault(address.getIsDefault());
        detailBo.setAreaId(address.getAreaId());
        detailBo.setAddress(address.getAddress());
        detailBo.setName(address.getName());
        detailBo.setMobile(address.getMobile());
        detailBo.setId(address.getId());
        detailBo.setCityId(address.getCityId());
        detailBo.setProvinceId(address.getProvinceId());
        detailBo.setProvinceName(region1.getName());
        detailBo.setCityName(region.getName());
        detailBo.setAreaName(address.getAddress());
        return detailBo;
    }

}

